---------------------------------------------------------------
-- @Copyright：           zour
-- @Description：         广告公共函数
-- @modify time           2021-09-26
---------------------------------------------------------------
require "modules.business_advert.advert_define"

local FOO = {
	_VERSION = '0.1.0',
};

--[[
	fixme:
		1.日志打印等级，error, warning, debug, info
		2.日志打印行号，函数名
]]

FOO.log_debug = function(title, body)
    local source = debug.getinfo(2).source
    local currentline = debug.getinfo(2).currentline
    if body~=nil then 
        log.debug("####["..source..":"..currentline..":]====>  "..title..serpent.block(body))
    else 
        log.debug("#####["..source..":"..currentline..":]====>  "..title)
    end
end

--table序列化
FOO.serpent_block = function(t)
	serpent.block(t)
end

--table转string
FOO.table2string = function(t)
	local mark={}
	local assign={}
	local function ser_table(tbl,parent)
		mark[tbl]=parent
		local tmp={}
		for k,v in pairs(tbl) do
			local key= type(k)=="number" and "["..k.."]" or "[".. string.format("%q", k) .."]"
			if type(v)=="table" then
				local dotkey= parent .. key
				if mark[v] then
					table.insert(assign,dotkey .. "=" .. mark[v] .. "")
				else
					table.insert(tmp, key .. "=" .. ser_table(v,dotkey))
				end
			elseif type(v) == "string" then
				table.insert(tmp, key .. "=" .. string.format('%q', v))
			elseif type(v) == "number" or type(v) == "boolean" then
				table.insert(tmp, key .. "=" .. tostring(v))
			end
		end
		return "{" .. table.concat(tmp,",") .. "}"
	end
	return ser_table(t,"ret") .. table.concat(assign,"")
end

--将table写入到test文件[易读]
FOO.write_test_file = function(t, filename)
	if not filename then 
		filename = "test"
	end
	filename = "./lua/modules/business_advert/"..filename..".lua"

	local file = io.open (filename, "w+")

	if type(t)=="table" then
		file:write(serpent.block(t))
	elseif type(t)=="string" then 
		file:write(t)
	end
	
	file:close()
end

--ts转date
FOO.ts2date = function (ts)
	return os.date("%Y-%m-%d %H:%M:%S", ts)
end

--date转ts
FOO.data2ts = function (date)
	local n1,n2,n3 = string.match(date, "(%d+)-(%d+)-(%d+)") 
	--print(n1) print(n2) print(n3)
	return os.time({year=n1, month=n2,day=n3, hour=0, min=0, sec=0})
end

--当月总天数计算
FOO.get_month_days = function()
	local year = os.date("%Y", uu.now())
	local month = os.date("%m", uu.now()) + 1	-- 获取天数的目标月份需要+1
	local month_days = os.date("%d", os.time({year = year, month = month, day = 0}))  -- day为0则表示上个月的最后一天

	return month_days
end

--获取当天0点时间戳
FOO.get_day_zero = function(ts)
	--[[
	--方法一： 直接减去 时区偏移与过去的秒数
	local zero_ts = ts - (ts + TIME_ZONE_DELTA*ONE_HOUR_SEC)%(ONE_DAY_HOUR*ONE_HOUR_SEC)
	--]]

	--方案二： 取年月日， 时分秒给0，即得当天0点时间戳
	local ts_table = os.date("*t", ts)
	local zero_ts = os.time({year=ts_table.year, month=ts_table.month, day=ts_table.day,
		hour=0, min=0, sec=0})

	--[[
	local zero_date = os.date("%Y-%m-%d %H:%M:%S", zero_ts)
	log.debug("get_day_zero date= "..zero_date)
	log.debug("get_day_zero ts= "..zero_ts)
	--]]

	return zero_ts
end

--获取当周0点时间戳
FOO.get_week_zero = function(ts)
	--!*t格林威治时间  *t当前时区时间
	local ts_table = os.date("*t", ts)
	local day_zero_ts = os.time({year=ts_table.year, month=ts_table.month, day=ts_table.day, 
		hour=0, min=0, sec=0})

	local week_zero_ts = day_zero_ts - (ts_table.wday-RESET_SUNDAY)*ONE_DAY_HOUR*ONE_HOUR_SEC

	--[[
	local week_zero_date = os.date("%Y-%m-%d %H:%M:%S", week_zero_ts)
	log.debug("get_week_zero date= "..week_zero_date)
	log.debug("get_week_zero ts= "..week_zero_ts)
	--]]

	return week_zero_ts
end

--[定时器]延迟执行一个任务
FOO.timer_at = function( delay_, func_, params_ )
    local timer_ = CNgxTimerWrap:new()
    return timer_:ngx_timer_at( delay_, func_,  params_ )
end

--[定时器]定期执行一个任务
FOO.timer_every = function( delay_, func_, params_ )
    local timer_ = CNgxTimerWrap:new()
    return timer_:ngx_timer_every( delay_, func_, params_  )
end

--拉取外部网页
FOO.get_http_page = function(url)
	if not url then 
		log.day_list(ADVERT, "get_http_page url=nil! ")
		return ERROR_CODE.OR_URL_ERROR
	end
    local ret, body = ns_network.getHttpPage( url )
    if ret==200 then
        if type(body)=='table' then 
			--uu.var_dump(body)
            body = table.tostring(body)
		elseif type(body)=='string' then 
			--log.debug( body )
		end
    else
        log.day_list(ADVERT, "get_http_page error! ret="..ret.."  url="..url)
        return ERROR_CODE.OR_GET_HTTP_FAIL
    end
   
    return ERROR_CODE.OR_SUCCESS, body
end

--大数据要的打印格式
FOO.op_business_advert = function (content)
	log.record_day("op_business_advert", content)
end

return FOO